const { QNConsts } = requirePlugin("QNBleApi");
const { sealGenderParam, isValidEightMeasure } = require("../../util/util");

// 指标图标名称对应
const iconNames = {
  'fer-Weight': 'report_weight',
  'fer-Stature': 'report_stature',
  'fer-WeightControl': 'report_weightControl',
  'fer-FatControl': 'report_fatControl',
  'fer-SinewControl': 'report_sinewControl',
  'fer-BMR': 'report_bmr',
  'fer-BestVisualWeight': 'report_bestVisualWeight',
  'fer-Bodyfat': 'report_bodyfat',
  'fer-BodyfatMass': 'report_bodyfatMass',
  'fer-BMI': 'report_bmi',
  'fer-BodyAge': 'report_bodyage',
  'fer-BodyShape': 'report_bodyshape',
  'fer-Bone': 'report_bone',
  'fer-HeartIndex': 'report_heart_index',
  'fer-HeartRate': 'report_heart_rate',
  'fer-LBM': 'report_lbm',
  'fer-MineralSalt': 'report_mineralSalt',
  'fer-Muscle': 'report_muscle',
  'fer-Sinew': 'report_muscle_mass',
  'fer-SinewRate': 'report_sinewRate',
  'fer-Obesity': 'report_obesity',
  'fer-Protein': 'report_protein',
  'fer-ProteinMass': 'report_proteinMass',
  'fer-Score': 'report_score',
  'fer-StandardWeight': 'report_standardWeight',
  'fer-Subfat': 'report_subfat',
  'fer-Visfat': 'report_visfat',
  'fer-Water': 'report_water',
  'fer-WaterMass': 'report_waterMass',
  'fer-FattyLiver': 'report_fattyLiver',
}

Component({
  options: {
    multipleSlots: true, // 在组件定义时的选项中启用多slot支持
  },

  /**
   * 组件的属性列表
   */
  properties: {
    measure: {
      type: Object,
      value: {},
    },
    hideNameDes: {
      type: Boolean,
      value: false,
    },
    eightMeasureData: {
      type: Object,
      value: {},
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    eightDeepReportShow: false,
    iconNames,
  },

  /**
   * 组件的方法列表
   */

  observers: {
    eightMeasureData: function (data) {
      if (!data) return;
      // 设备类型为八电级且测出了节段数据
      if (isValidEightMeasure(data)) {
        this.setData({
          eightDeepReportShow: true,
        });
      }
    },
    measure: function (data) {
      if (!data) return;
      if (this.data.eightDeepReportShow) {
        this.setData({
          eightDeepReportShow: false,
        });
      }
    },
  },

  methods: {
    //点击列表显示隐藏
    onClickToggleExtend: function (e) {
      const { measure } = this.data;
      const index = e.currentTarget.dataset.index;
      if (
        !measure[index].desc &&
        !measure[index].boundaries
      )
        return;
      measure[index].isExtend = !measure[index]
        .isExtend;
      this.setData({
        measure,
      });
    },
    jumpToDeepReport() {
      const measure = this.data.eightMeasureData;
      const { user } = measure;

      const measureData = {
        birthday: user.birthday,
        gender: sealGenderParam(user.gender),
        height: user.height,
        bmi: measure.bmi,
        bmr: measure.bmr,
        body_age: measure.bodyAge,
        bodyfat: measure.bodyfat,
        // 八电级下发的是脂肪量，需要转换为脂肪率
        bodyfat_left_arm: measure.fatLh,
        bodyfat_left_leg: measure.fatLf,
        bodyfat_right_arm: measure.fatRh,
        bodyfat_right_leg: measure.fatRf,
        bodyfat_trunk: measure.fatT,
        bone: measure.bone,
        lbm: measure.lbm,
        muscle: measure.muscle,
        protein: measure.protein,
        // 注意这个字段
        sinew: measure.muscleMass,
        sinew_left_arm: measure.muscleLh,
        sinew_left_leg: measure.muscleLf,
        sinew_right_arm: measure.muscleRh,
        sinew_right_leg: measure.muscleRf,
        sinew_trunk: measure.muscleT,
        subfat: measure.subfat,
        visfat: measure.visfat,
        water: measure.water,
        weight: measure.weight,
      };

      console.log('八电级显示数据', measureData);
      wx.navigateTo({
        url: `/pages/deepReport/index?measureData=${JSON.stringify(
          measureData
        )}`,
      });
    },
  },
});
